package com.mz361.admin.manage.controller;

import com.mz361.common.enums.StatusEnum;
import com.mz361.common.utils.ResultVoUtil;
import com.mz361.common.utils.StatusUtil;
import com.mz361.common.vo.ResultVo;
import com.mz361.modules.system.domain.CustNeeds;
import com.mz361.modules.system.service.CustNeedsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author niceale
 * @date 2024/07/19
 */
@Controller
@ApiIgnore
@RequestMapping("/system/custNeeds")
public class CustNeedsController {

    @Autowired
    private CustNeedsService custNeedsService;

    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("system:custNeeds:index")
    public String index(Model model, CustNeeds custNeeds) {

        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("needs", match -> match.contains());

        // 获取数据列表
        Example<CustNeeds> example = Example.of(custNeeds, matcher);
        Page<CustNeeds> list = custNeedsService.getPageList(example);

        // 封装数据
        model.addAttribute("list", list.getContent());
        model.addAttribute("page", list);
        return "/system/custNeeds/index";
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("system:custNeeds:detail")
    public String toDetail(@PathVariable("id") CustNeeds custNeeds, Model model) {
        model.addAttribute("custNeeds", custNeeds);
        return "/system/custNeeds/detail";
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/status/{param}")
    @RequiresPermissions("system:custNeeds:status")
    @ResponseBody
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Long> ids) {
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        if (custNeedsService.updateStatus(statusEnum, ids)) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }
}